package com.swipemaster.dialogs;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.swipemaster.R;
import com.swipemaster.common.Global;
import com.swipemaster.network.Network;
import com.swipemaster.network.WorldRecord;
import com.swipemaster.network.task.DownloadMoreRecordsTask;
import com.swipemaster.record.RecordId;
import com.swipemaster.record.RecordsManager;

public class RecordsDialog extends Dialog implements OnClickListener
{
	private final Button exitBtn;
	private final Button showMoreBtn;
	private final Button submitScoreBtn;
	private final TextView titleTextView;
	private final TextView userScoreTextView;
	private static ListView listView;
	private static RecordsListAdapter adapter;
	private static Activity activity;
	private static RecordId recordId;
	private final static int SLIDE_BUTTON_ALPHA = 190;

	public RecordsDialog(Activity activity, RecordId recordId, ArrayList<WorldRecord> listOfRecords)
	{
		super(activity);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.high_scores_dialog);

		RecordsDialog.activity = activity;
		RecordsDialog.recordId = recordId;

		getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
		getWindow().getAttributes().windowAnimations = R.style.DialogAnimationScale;

		titleTextView = (TextView) findViewById(R.id.level_high_scores_title);
		titleTextView.setText(recordId.toString());
		titleTextView.setTextColor(Color.WHITE);

		userScoreTextView = (TextView) findViewById(R.id.level_high_scores_user_time);
		if (RecordsManager.getRecord(recordId).isValid())
		{
			userScoreTextView.setText(Global.app.getString(R.string.your_record) + " "
					+ RecordsManager.getRecord(recordId));
		}
		else
		{
			userScoreTextView.setText(Global.app.getString(R.string.your_record) + " "
					+ Global.app.getString(R.string.no_record));
		}
		userScoreTextView.setTextColor(Color.WHITE);

		exitBtn = (Button) findViewById(R.id.exit_level_high_scores_btn);
		exitBtn.setOnClickListener(this);
		showMoreBtn = (Button) findViewById(R.id.show_more_results_btn);
		showMoreBtn.setOnClickListener(this);
		showMoreBtn.getBackground().setAlpha(SLIDE_BUTTON_ALPHA);

		submitScoreBtn = (Button) findViewById(R.id.level_submit_score_btn);
		submitScoreBtn.setOnClickListener(this);
		submitScoreBtn.getBackground().setAlpha(SLIDE_BUTTON_ALPHA);
		if (!RecordsManager.getRecord(recordId).isValid())
		{
			submitScoreBtn.setVisibility(View.INVISIBLE);
		}

		listView = (ListView) findViewById(R.id.listView1);
		adapter = new RecordsListAdapter(activity);
		adapter.addRecords(listOfRecords);
		listView.setDivider(null);
		listView.setScrollbarFadingEnabled(false);
		listView.setAdapter(adapter);

		Dialogs.ensureSize(findViewById(R.id.level_high_scores_layout));
	}

	/** Downloads and replaces records in dialog if necessary
	 * @param userRank position of player score in global ranking */
	public static void refreshList(int userRank)
	{
		if ((adapter == null) || (listView == null))
		{
			return;
		}
		if (userRank <= adapter.getCount())
		{
			try
			{
				final ArrayList<WorldRecord> refreshedRecords = Network
						.getWorldRecords(recordId, 0, adapter.getCount());
				adapter.clear();
				adapter.addRecords(refreshedRecords);
				activity.runOnUiThread(new Runnable()
				{
					@Override
					public void run()
					{
						listView.invalidateViews();
					}
				});
			}
			catch (final Exception e)
			{
				// If there was internet error during refreshing list , we just leave it.
				e.printStackTrace();
				return;
			}
		}
	}

	@Override
	public void onClick(View view)
	{
		if (view == exitBtn)
		{
			dismiss();
		}
		else if (view == showMoreBtn)
		{
			new DownloadMoreRecordsTask(activity, listView, adapter, recordId).execute();
		}
		else if (view == submitScoreBtn)
		{
			Dialogs.showEntryTextDialog(activity, recordId);
		}
	}
}